{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 仿生蜘蛛基本运动控制\n",
    "\n",
    "## 硬件准备\n",
    "\n",
    "材料：\n",
    "\n",
    "功能模块F*1、T模块*8\n",
    "\n",
    "\n",
    "\n",
    "## 硬件连接\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "## 关节配置\n",
    "\n",
    "将硬件连接的关节ID次序更新到配置文件中\n",
    "\n",
    "\n",
    "\n",
    "配置完后启动设备\n",
    "\n",
    "\n",
    "# 代码说明\n",
    "\n",
    "### 导入库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/python\n",
    "# -*- coding: UTF-8 -*-\n",
    "import sys;\n",
    "sys.path.append(\"..\")\n",
    "sys.path.append(\"../../\")\n",
    "from lib.BwRobotLib import BwRobotLib\n",
    "from RobotSpider.SpiderRobot import SpiderRobot\n",
    "import keyboard\n",
    "import time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 声明库对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#声明库对象\n",
    "robotlib = BwRobotLib()\n",
    "spiderRobot = SpiderRobot(robotlib)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 连接设备"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "连接成功\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#连接机器人设备\n",
    "robotlib.connectRobot(\"192.168.1.107\",55000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 配置并初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "读取ID配置文件:\n",
      "L1:30,31\n",
      "\n",
      "L3:32,33\n",
      "\n",
      "R1:34,35\n",
      "\n",
      "R2:36,37\n",
      "\n",
      "机器人初始化设置成功\n"
     ]
    }
   ],
   "source": [
    "#加载配置（兼容VS项目启动方式及文件直接启动方式）\n",
    "import os,inspect\n",
    "fileName = inspect.getframeinfo(inspect.currentframe()).filename\n",
    "folderPath     = os.path.dirname(os.path.abspath(fileName))\n",
    "cfile=folderPath+\"/config.txt\"\n",
    "robotlib.loadConfig(cfile)\n",
    "\n",
    "#配置ID\n",
    "spiderRobot.bindIds(robotlib.module_ids)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 键盘控制处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#处理键盘\n",
    "#处理键盘\n",
    "def handleControlKeys(key):\n",
    "    isDown = key.event_type == 'down'\n",
    "    if not isDown:\n",
    "        return\n",
    "\n",
    "    cmd = key.name\n",
    "    if(cmd == '1'):\n",
    "        spiderRobot.goForward()        \n",
    "    if(cmd == '2'):\n",
    "        spiderRobot.goBack()\n",
    "    if(cmd == '3'):\n",
    "        spiderRobot.goForward_down()\n",
    "    if(cmd == '4'):\n",
    "        spiderRobot.goBack_down()\n",
    "    if(cmd == '5'):\n",
    "        spiderRobot.turnLeft()\n",
    "    if(cmd == '6'):\n",
    "        spiderRobot.turnRight()\n",
    "    if(cmd == '7'):\n",
    "        spiderRobot.turnLeft_down()\n",
    "    if(cmd == '8'):\n",
    "        spiderRobot.turnRight_down()\n",
    "    if(cmd == '9'):\n",
    "        spiderRobot.doUpAndDown()\n",
    "    if(cmd == 'a'):\n",
    "        spiderRobot.sayHello()\n",
    "    if(cmd == 'b'):\n",
    "        spiderRobot.doSport()\n",
    "    if(cmd == 'c'):\n",
    "        spiderRobot.doSport_oneHand()\n",
    "    if(cmd == 'd'):\n",
    "        spiderRobot.dance()\n",
    "    if(cmd == 'e'):\n",
    "        spiderRobot.doHangTail()\n",
    "    if(cmd == 'f'):\n",
    "        spiderRobot.doBattle()\n",
    "    if(cmd == 'g'):\n",
    "        spiderRobot.doMoveBody()\n",
    "    pass\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 控制提示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "#控制提示\n",
    "def controlTips():\n",
    "    print(\"键盘控制命令 :\")\n",
    "    print(\"1 : 前进\")\n",
    "    print(\"2 : 后退\")\n",
    "    print(\"3 : 匍匐前进\")\n",
    "    print(\"4 : 匍匐后退\")\n",
    "    print(\"5 : 左转\")\n",
    "    print(\"6 : 右转\")\n",
    "    print(\"7 : 匍匐左转\")\n",
    "    print(\"8 : 匍匐右转\")\n",
    "    print(\"9 : 蹲下起立\")\n",
    "    print(\"a : 打招呼\")\n",
    "    print(\"b : 俯卧撑\")\n",
    "    print(\"c : 单手俯卧撑\")\n",
    "    print(\"d : 跳舞\")\n",
    "    print(\"e : 摇尾巴\")\n",
    "    print(\"f : 战斗防御\")\n",
    "    print(\"g : 扭动身体防御 \")\n",
    "    print(\"-----------------------\")\n",
    "    print(\"0 :Exit\")\n",
    "    keyboard.hook(handleControlKeys)\n",
    "    #参考Python 键盘/鼠标监听及控制：https://www.jianshu.com/p/8e508c6a05ce\n",
    "    #helper.Wait(True)\n",
    "    keyboard.wait('0')\n",
    "    keyboard.unhook_all()\n",
    "\n",
    "    pass\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 主入口"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "键盘控制命令 :\n",
      "1 : 前进\n",
      "2 : 后退\n",
      "3 : 匍匐前进\n",
      "4 : 匍匐后退\n",
      "5 : 左转\n",
      "6 : 右转\n",
      "7 : 匍匐左转\n",
      "8 : 匍匐右转\n",
      "9 : 蹲下起立\n",
      "a : 打招呼\n",
      "b : 俯卧撑\n",
      "c : 单手俯卧撑\n",
      "d : 跳舞\n",
      "e : 摇尾巴\n",
      "f : 战斗防御\n",
      "g : 扭动身体防御 \n",
      "-----------------------\n",
      "0 :Exit\n"
     ]
    }
   ],
   "source": [
    "#为防止与JupterLab的快捷键冲突，请最小化当前进程或在其它程序窗口执行以下命令\n",
    "controlTips()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
